﻿'Imports userModel
Imports System
Imports ADOEntity

Partial Class AML_changePassword
    Inherits Web.UI.BasePage

    Dim clsEdmUser As New clsEdmUser
    Dim clsMenuRight As New clsSessionMenuRight
    Dim strMsg As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim UserEntity As New UserEntities
        clsMenuRight = Session("ClsMenuRight")

        If Not IsPostBack Then
            chkIsLogin()
            txtUsername.Text = clsMenuRight.getUserName
            If clsMenuRight.getIsPwdExpire = 1 Or clsMenuRight.getMustChangePwd = 1 Then
                If clsMenuRight.getRoleGroup < 70 Then
                    If clsMenuRight.getRoleGroup < 70 And clsMenuRight.getRoleGroup > 59 Then
                        'Dim qryUser = From tblUser In UserEntity.SUSER
                        'Where tblUser.

                    ElseIf clsMenuRight.getRoleGroup < 60 And clsMenuRight.getRoleGroup > 49 Then

                    ElseIf clsMenuRight.getRoleGroup < 49 Then
                        Dim qryUser = From tblUser In UserEntity.SUSER

                    End If
                End If

            End If
            CreateControlMP()
        End If
    End Sub

    Sub CreateControlMP()
        'Dim Menu1 As Menu = CType(Master.FindControl("Menu1"), Menu)
        'Menu1.Items(0).Enabled = False
        'Menu1.Items(1).Enabled = False
        'Menu1.Items(2).Enabled = False
        'Menu1.Items(3).Enabled = False
        'Menu1.Items(4).Enabled = False
        'Menu1.Items(5).Enabled = False

        'For count As Integer = 0 To Menu1.Items.Count - 1
        '    If (Menu1.Items(count).Enabled = False) Then
        '        Menu1.Items(count).Text = ""
        '        Menu1.Items(count).ImageUrl = ""
        '    End If
        'Next

        '  "เปลี่ยนรหัสผ่าน"
        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "เปลี่ยนรหัสผ่าน"
        Dim pageForm As Label = CType(Master.FindControl("lblForm"), Label)
        pageForm.Text = "เปลี่ยนรหัสผ่าน"
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        clsMenuRight = Session("ClsMenuRight")
        If (chkpassword()) Then
            If Not clsMenuRight.getUserName = "" Then
                If (clsEdmUser.saveChangePassword(clsMenuRight.getUserName, txtOldPwd.Text, txtNewPwd.Text)) Then
                    clsMenuRight.getIsPwdExpire = 0
                    clsMenuRight.getMustChangePwd = 0
                    Session("ClsMenuRight") = clsMenuRight
                    Response.Redirect("News.aspx")
                Else
                    If Not (clsEdmUser.ErrExp = "") Then
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", String.Format("alert('{0}')", clsEdmUser.ErrExp), True)
                    Else
                        If Not (clsEdmUser.Msg = "") Then
                            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", String.Format("alert('{0}')", clsEdmUser.Msg), True)

                        End If
                    End If
                End If
            Else

            End If
        Else
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", String.Format("alert('{0}')", strMsg), True)
        End If

        clsMenuRight = Nothing
    End Sub

    Private Function chkpassword() As Boolean
        Dim chkstatus As Boolean = True
        Dim clsCheckPwd As clsCheckPwd = New clsCheckPwd
        Dim clsSystemConfig As clsSystemConfig = New clsSystemConfig

        If (txtNewPwd.Text.Trim = "" Or txtRePwd.Text.Trim = "") Then
            strMsg = String.Format("{0}{1}\n", strMsg, String.Format(Resources.resValidate.validate_TextLength, Resources.resUser.password))
            Return chkstatus = False
        Else
            ' lblChkBetweenPwd.Visible = False
        End If

        If (txtNewPwd.Text.Length > 7) Then
            If Not (txtNewPwd.Text.Trim = txtRePwd.Text.Trim) Then
                strMsg = String.Format("{0}{1}\n", strMsg, Resources.resValidate.validate_TextPassword)
                Return chkstatus = False
            Else
                '   lblChkPassword.Visible = False
            End If
        Else
            strMsg = String.Format("{0}{1}\n", strMsg, String.Format(Resources.resValidate.validate_TextLength, Resources.resUser.password))
            Return chkstatus = False
        End If

        If Not (clsCheckPwd.checkpassword(txtNewPwd.Text)) Then
            strMsg = String.Format("{0}{1}\n", strMsg, Resources.resValidateUser.NotPasswordStrong)
            chkstatus = False
        End If

        clsSystemConfig.setdefaultValueString()
        If (clsSystemConfig.chkDupPwd(txtNewPwd.Text)) Then
            strMsg = String.Format("{0}{1}\n", strMsg, Resources.resValidateUser.PasswordDuplicate)
            chkstatus = False
        End If

        clsSystemConfig = Nothing


        Return chkstatus
    End Function

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If (clsMenuRight.getIsPwdExpire = "" Or clsMenuRight.getIsPwdExpire = "0") And (clsMenuRight.getMustChangePwd = "" Or clsMenuRight.getMustChangePwd = "0") Then
            Response.Redirect("News.aspx")
        Else
            Session.Abandon()
            Response.Redirect("index.aspx")

        End If
    End Sub
End Class
